/*
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "./lap.h"
#include <time.h>

Lap::Lap(D1015* r)
{
    time_t time  = TIME_OFFSET + r->start_time;
    tm *t      = localtime(&time);
    // tm_year: years since 1900
    start_date = QDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
    start_time = QTime(t->tm_hour, t->tm_min, t->tm_sec);
    total_time = QTime().addSecs(r->total_time / 100);
    
    index      = r->index;
    total_dist = r->total_dist;
    max_speed  = r->max_speed;
    begin      = Position((position_type)r->begin);
    end        = Position((position_type)r->end);
    calories   = r->calories;
    avg_heart_rate = r->avg_heart_rate;
    max_heart_rate = r->max_heart_rate;
    intensity      = r->intensity;
    avg_cadence    = r->avg_cadence;
    trigger_method = r->trigger_method;
}

Lap::Lap()
{
    index      = 0;
    start_time = QTime();
    start_date = QDate();
    total_time = QTime();
    total_dist = 0;
    max_speed  = 0;
    begin      = Position();
    end        = Position();
    calories   = 0;
    avg_heart_rate = 0;
    max_heart_rate = 0;
    intensity      = 0;
    avg_cadence    = 0;
    trigger_method = 0;
}

QString Lap::toString() {
  QString string("[ LAP");
  string += " index: ";
  string += QVariant(index).toString();
  string += " start time: ";
  string += start_time.toString();
  string += " start date: ";
  string += start_date.toString();
  string += " total time: ";
  string += total_time.toString();
  string += " total_distance: ";
  string += QVariant(total_dist).toString();
  string += " start: ";
  string += begin.toString();
  string += " end: ";
  string += end.toString();
  string += " max_speed: ";
  string += QVariant(max_speed).toString();

  // not printing for some reason
  if (calories > 0 && calories < 5000) {
    string += " calories: ";
    string += QVariant(calories).toString();
  }
  if (avg_heart_rate > 0) {
    string += " avg heart rate: ";
    string += QVariant(avg_heart_rate).toString();
  }
  if (max_heart_rate > 0) {
    string += " max heart rate: ";
    string += QVariant(max_heart_rate).toString();
  }
  if (intensity > 0) {
    string += " intensity: ";
    string += QVariant(intensity).toString();
  }
  if (avg_cadence > 0 && avg_cadence < 100) {
    string += " avg cadence: ";
    string += QVariant(avg_cadence).toString();
  }
  if (trigger_method > 0) {
    string += " trigger method: ";
    string += QVariant(trigger_method).toString();
  }
  string += " ]";
  return string;
}


//
//
//  getters/setters
//
//

QDate Lap::getDate()
{
  return start_date;
}

uint16 Lap::getIndex()
{
    return index;
}
void Lap::setIndex(uint16 val)
{
    index = val;
}

QTime Lap::getStart_time()
{
    return start_time;
}
void Lap::setStart_time(QTime val)
{
    start_time = val;
}
QTime Lap::getTotal_time()
{
    return total_time;
}
void Lap::setTotal_time(QTime val)
{
    total_time = val;
}
float32 Lap::getTotal_dist()
{
    return total_dist;
}
void Lap::setTotal_dist(float32 val)
{
    total_dist = val;
}
float32 Lap::getMax_speed()
{
    return max_speed;
}
void Lap::setMax_speed(float32 val)
{
    max_speed = val;
}
Position Lap::getBegin()
{
    return begin;
}
// TODO: copy
// TODO: also, by reference
void Lap::setBegin(Position val)
{
    begin = val;
}
Position Lap::getEnd()
{
    return end;
}
void Lap::setEnd(Position val)
{
    end = val;
}
uint16 Lap::getCalories()
{
    return calories;
}
void Lap::setCalories(uint16 val)
{
    calories = val;
}
uint8 Lap::getAvg_heart_rate()
{
    return avg_heart_rate;
}
void Lap::setAvg_heart_rate(uint8 val)
{
    avg_heart_rate = val;
}
uint8 Lap::getMax_heart_rate()
{
    return max_heart_rate;
}
void Lap::setMax_heart_rate(uint8 val)
{
    max_heart_rate = val;
}
uint8 Lap::getIntensity()
{
    return intensity;
}
void Lap::setIntensity(uint8 val)
{
    intensity = val;
}
uint8 Lap::getAvg_cadence()
{
    return avg_cadence;
}
void Lap::setAvg_cadence(uint8 val)
{
    avg_cadence = val;
}
uint8 Lap::getTrigger_method()
{
    return trigger_method;
}
void Lap::setTrigger_method(uint8 val)
{
    trigger_method = val;
}
Track Lap::getTrack()
{
  return track;
}
void  Lap::setTrack(Track t)
{
  track = t;
}
